/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import junit.framework.TestCase;




/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   public UrlValidatorTest(String testName) {
      super(testName);
   }

   
   
   public void testManualTest()
   {
	   System.out.println("Starting Manual Testing........");
	   
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   System.out.println(urlVal.isValid("http://www.amazon.com"));
	   System.out.println(urlVal.isValid("//::http://www.amazon.com/ee124532"));
	   System.out.println(urlVal.isValid("http://www.amazon.com"));
	   System.out.println(urlVal.isValid(".com//hello_sir.com.net://https://www."));
	   
	   System.out.println("Manual Tests Complete.......");
   }
   
   
   public void testYourFirstPartition()
   {
	   //some to be correct some to be false
	   UrlValidator vlad = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
	   System.out.println("Starting First Partition Test........");
	   
	   String[] scheme;
	   scheme = new String[3];
	   scheme[0] = "http://";
	   scheme[1] = "https://";
	   scheme[2] = "ftp://";
	   scheme[3] = "";
	   
	   String[] host;
	   host = new String[3];
	   host[0] = "amazon.com";
	   host[1] = "google.com";
	   host[2] = "yahoo.com";
	   host[3] = "200.200.200.200";

	   String[] port;
	   port = new String[3];
	   port[0] = ":100";
	   port[1] = ":500";
	   port[2] = ":0";
	   port[3] = ":65535";
	   
	   String[] path;
	   path = new String[3];
	   path[0] = "/";
	   path[1] = "/products";
	   path[2] = "/news";
	   path[3] = "";
	   
	   String[] query;
	   query = new String[3];
	   query[0] = "";
	   query[1] = "?p=something";
	   query[2] = "?d=10&e=5";
	   query[3] = "";
	   
	   String foo_1 = scheme[0] + host[0] + port[0] + path[0] + query[0];
	   String foo_2 = scheme[1] + host[1] + port[1] + path[1] + query[1];
	   String foo_3 = scheme[2] + host[2] + port[2] + path[2] + query[2];
	   String foo_4 = scheme[3] + host[3] + port[3] + path[3] + query[3];
	   
	   System.out.println(vlad.isValid(foo_1));
	   System.out.println(vlad.isValid(foo_2));
	   System.out.println(vlad.isValid(foo_3));
	   System.out.println(vlad.isValid(foo_4));
	   
	   System.out.println("First Partition Done........");
   }
   
   public void testYourSecondPartition()
   {
	   UrlValidator bald = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);

	   System.out.println("Starting Second Partition Testing........");
	   
	   String[] scheme;
	   scheme = new String[3];
	   scheme[0] = "http:/";
	   scheme[1] = "hpv://";
	   scheme[2] = "std://";
	   scheme[3] = "http//";
	   
	   String[] host;
	   host = new String[3];
	   host[0] = "amazon...com";
	   host[1] = "google";
	   host[2] = "yahoo./com";
	   host[3] = "300.300.300.300";

	   String[] port;
	   port = new String[3];
	   port[0] = ":-1";
	   port[1] = ":abc";
	   port[2] = "::";
	   port[3] = ":70000";
	   
	   String[] path;
	   path = new String[3];
	   path[0] = "/&&&&^^";
	   path[1] = "//*^%products";
	   path[2] = "(=$#/n_e_ws";
	   path[3] = "\\";
	   
	   String[] query;
	   query = new String[3];
	   query[0] = "";
	   query[1] = "?p=999988***&^";
	   query[2] = "?d=10&e=5";
	   query[3] = "?==";
	   
	   String foo_1 = scheme[0] + host[0] + path[0] + query[0];
	   String foo_2 = scheme[1] + host[1] + path[1] + query[1];
	   String foo_3 = scheme[2] + host[2] + path[2] + query[2];
	   String foo_4 = scheme[3] + host[3] + path[3] + query[3];
	   
	   System.out.println(bald.isValid(foo_1));
	   System.out.println(bald.isValid(foo_2));
	   System.out.println(bald.isValid(foo_3));
	   System.out.println(bald.isValid(foo_4));
	   
	   System.out.println("Second Test Partitioning Done.......");
   }
   
   public void testIsValid()
   {
	   System.out.println("TEST IS VALID TEST STARTING.........");
	   
	   ResultPair[] scheme = {new ResultPair("http://", true),
               new ResultPair("ftp://", true),
               new ResultPair("h3t://", true),
               new ResultPair("3ht://", false),
               new ResultPair("http:/", false),
               new ResultPair("http:", false),
               new ResultPair("http/", false),
               new ResultPair("://", false),
               new ResultPair("", true)};
	   
	   ResultPair[] url = {new ResultPair("www.google.com", true),
               new ResultPair("go.com", true),
               new ResultPair("go.au", true),
               new ResultPair("0.0.0.0", true),
               new ResultPair("255.255.255.255", true),
               new ResultPair("256.256.256.256", false),
               new ResultPair("255.com", true),
               new ResultPair("1.2.3.4.5", false),
               new ResultPair("1.2.3.4.", false),
               new ResultPair("1.2.3", false),
               new ResultPair(".1.2.3.4", false),
               new ResultPair("go.a", false),
               new ResultPair("go.a1a", false),
               new ResultPair("go.cc", true),
               new ResultPair("go.1aa", false),
               new ResultPair("aaa.", false),
               new ResultPair(".aaa", false),
               new ResultPair("aaa", false),
               new ResultPair("", false)};
	   
	   ResultPair[] port = {new ResultPair(":80", true),
               new ResultPair(":65535", true),
               new ResultPair(":1000", true),
               new ResultPair(":0", true),
               new ResultPair("", true),
               new ResultPair(":-1", false),
               new ResultPair(":65636", false),
               new ResultPair(":65a", false)};
	   
	   ResultPair[] path = {new ResultPair("/test1", true),
               new ResultPair("/t123", true),
               new ResultPair("/$23", true),
               new ResultPair("/..", false),
               new ResultPair("/../", false),
               new ResultPair("/test1/", true),
               new ResultPair("", true),
               new ResultPair("/test1/file", true),
               new ResultPair("/..//file", false),
               new ResultPair("/test1//file", false)};
	   
	   //false every time there is a query in the string
	   ResultPair[] query = {new ResultPair("?action=view", true),
               new ResultPair("?action=edit&mode=up", true),
               new ResultPair("", true),
               new ResultPair("?****========&*************", false)};
	   
	   for(int i = 0;i<10000;i++)
	   {
		   int scheme_num = (int)(Math.random() * 8);
		   int url_num = (int)(Math.random() * 18);
		   int port_num = (int)(Math.random() * 7);
		   int path_num = (int)(Math.random() * 9);
		   int query_num = (int)(Math.random() * 3);
		   
		   String foo_test = scheme[scheme_num].item + url[url_num].item + port[port_num].item + path[path_num].item + query[query_num].item;
		   
		   boolean expected_result = scheme[scheme_num].valid && url[url_num].valid && port[port_num].valid && path[path_num].valid && query[query_num].valid;	   
		   
		   UrlValidator checking = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
		  
		   boolean result = checking.isValid(foo_test);
		   
		   if(result != expected_result)
		   {
			   if(expected_result)
			   {
				   System.out.println("Expected True, Got False");
				   System.out.println(foo_test);
			   }
			   else
			   {
				   System.out.println("Expected False, Got True");
				   System.out.println(foo_test);
			   }
		   }
	   }
	   System.out.println("TEST IS VALID TEST DONE.........");
	   
   }
   
   public void testAnyOtherUnitTest()
   {
	   
   }
   /**
    * Create set of tests by taking the testUrlXXX arrays and
    * running through all possible permutations of their combinations.
    *
    * @param testObjects Used to create a url.
    */
   

}
